<?php

namespace app\admin\controller;

use app\common\Error;
use think\facade\Request;
use app\common\Common;
use app\admin\service\UserService;

// 后台管理员
class Admin extends Base
{
    private $service;

    public function __construct(UserService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    /**
     * 渲染列表模版
     */
    public function index()
    {
        return view();
    }

    /**
     * 渲染列表模版
     */
    public function info()
    {
        return view();
    }

    // 管理员列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getManagerList($params);

        return Common::success($data);
    }

    // 管理员详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Admin::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getManagerDetail($params['id']);

        return Common::success($data);
    }

    /**
     * 添加管理员
     */
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Admin::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editManager($params);

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 修改管理员
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Admin::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editManager($params);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    /**
     * 删除管理员
     * 
     * @author zhixiang0215@126.com
     * @param int $id
     * @return json
     */
    public function delete()
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Admin::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deleteManager($params['id']);

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::DELETE_FAIL;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 管理员下拉列表
    public function select()
    {
        $data = $this->service->getManagerSelectList();
        return Common::success($data);
    }

    // 开启状态账户可登录
    public function setStatus()
    {
        $params = Request::param();

        $data = $this->service->isSetStatus($params);

        return Common::success($data);
    }
}